package com.itfreer.arcgis.edit;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.itfreer.arcgis.draw.DrawCommand;
import com.itfreer.arcgis.draw.DrawToolManager;
import com.itfreer.arcgis.layer.LayerLoader;
import com.itfreer.arcgis.utils.FeatureTableUtils;
import com.itfreer.gis.datasyn.entity.ProjectEntity;
import com.itfreer.gis.datasyn.sub.EditLogService;
import com.itfreer.utils.common.ToastUtils;
import com.itfreer.utils.config.ConfigManager;


import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 创建新要素
 */
public class CreateNewFeatureCommand implements DrawCommand {

    @Override
    public void Execute(Map<String,Object> params) {
        if (DrawToolManager.GetCurrentTool() == null) {
            ToastUtils.showShort(ConfigManager.getContext(), "当前无编辑操作！");
            return;
        }
        FeatureLayer featureLayer = EditToolUtils.getCurrentEditFeatureLayer();
        if (featureLayer != null) {
            ProjectEntity config = LayerLoader.instance.getConfig(featureLayer);
            Map<String, Object> attribute = new HashMap<>();
            ValueOption.setDefultValue(featureLayer, attribute, config);
            String sguid = UUID.randomUUID().toString().replace("-", "");
            attribute.put("SGUID", sguid);
            Geometry geo = DrawToolManager.GetCurrentTool().GetGeometry();
            // 需改造
            ValueOption.updateArea(featureLayer, "MIAN_JI", attribute, geo);
            FeatureTableUtils.AddFeature(geo, attribute, featureLayer.getFeatureTable());
            // 记录日志
            EditLogService.instance.addLog(config, sguid);
            DrawToolManager.GetCurrentTool().Clear();
        } else {
            ToastUtils.showShort(ConfigManager.getContext(), "当前无可编辑图层");
        }
    }
}
